package com.boot.web.controller;

import java.util.Map;

import com.boot.web.dao.OrdersDao;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.boot.communal.paging.InitPaging;
import com.boot.communal.response.ResponseServer;
import com.boot.web.entity.Orders;
import com.boot.web.service.OrdersService;

/**
 * 订单控制器
 * @author 【zhouJie】
 * @date 2021-06-05 14:45:19
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrdersDao ordersDao;

    @PostMapping("/save")
    public ResponseServer save(Orders orders) {
        return ResponseServer.success("添加成功", ordersService.save(orders));
    }

    @PutMapping("/update")
    public ResponseServer update(Orders orders) {
        Orders o = ordersDao.queryById(orders.getId());
        if(orders.getLogisticsNumber() != null && !"".equals(orders.getLogisticsNumber())){
            if("1".equals(o.getDealStatus())){
                orders.setDealStatus("2");
            }else{
                if("0".equals(o.getDealStatus())){
                    return ResponseServer.error("当前订单未支付，不能发货");
                }else if("5".equals(o.getDealStatus())){
                    return ResponseServer.error("当前订单还未开奖，不能发货");
                }else{
                    if(!"2".equals(o.getDealStatus())){
                        return ResponseServer.error("当前订单状态不正确");
                    }
                }
            }
        }else{
            orders.setLogisticsNumber("");
        }
        ordersService.update(orders);
        return ResponseServer.success("修改成功");
    }

    @DeleteMapping("/delete/{id}")
    public ResponseServer delete(@PathVariable Integer id) {
        Orders orders = ordersDao.queryById(id);
        orders.setIsDel("1");
        ordersDao.update(orders);
        //ordersService.delete(id);
        return ResponseServer.success("删除成功");
    }

    @DeleteMapping("/batchDelete/{ids}")
    public ResponseServer batchDelete(@PathVariable Integer[] ids) {
        for(int i = 0; i < ids.length; i++){
            Orders orders = ordersDao.queryById(ids[i]);
            orders.setIsDel("1");
            ordersDao.update(orders);
        }
        //ordersService.batchDelete(ids)
        return ResponseServer.success("批量删除成功");
    }

    @GetMapping("/getPageList")
    public ResponseServer getPageList(@RequestParam Map<String, Object> params) {
        InitPaging.init(params);
        return ResponseServer.success(new PageInfo(ordersService.getOrdersVOPageList(params)));
    }

    @GetMapping("/getAll")
    public ResponseServer getAll() {
        return ResponseServer.success(ordersDao.queryAllVO());
    }

    @GetMapping("/getById/{id}")
    public ResponseServer getById(@PathVariable Integer id) {
        return ResponseServer.success(ordersService.getById(id));
    }

    /**
     * 上传物流单号
     * @param params
     * @return
     */
    @PostMapping("/uploadLogisticsNumber")
    public ResponseServer uploadLogisticsNumber(@RequestParam Map<String, Object> params) {
        String strJson = params.get("readList").toString();
        JSONArray jsonArray = JSONArray.fromObject(strJson);
        if (jsonArray.size() > 0) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                //Integer id = (Integer) jsonObject.get("订单ID");
                //Orders order = ordersDao.queryById(id);
                String orderNo = jsonObject.get("订单号").toString().replaceAll("\\s*", "");
                Orders order1 = ordersDao.queryByOrderNo(orderNo);
                //if (order != null && order1 != null) {
                if (order1 != null) {
                    if(jsonObject.containsKey("物流号")){
                        String logisticsNumber = jsonObject.get("物流号").toString().replaceAll("\\s*", "");
                        if (!"".equals(logisticsNumber)) {
                            if("1".equals(order1.getPayStatus()) && "1".equals(order1.getDealStatus()) && "0".equals(order1.getRefundStatus())){
                                order1.setLogisticsNumber(logisticsNumber);
                                order1.setDealStatus("2");
                                ordersDao.update(order1);
                            }
                        }
                    }
                }
            }
        }
        return ResponseServer.success();
    }

}
